package com.heartware.sh.blackhole.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.TextureView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.heartware.sh.blackhole.R;
import com.heartware.sh.blackhole.presenter.ILoginPresenter;
import com.heartware.sh.blackhole.presenter.impl.ILoginPresenterImpl;
import com.heartware.sh.blackhole.ui.activity.base.BaseActivity;
import com.heartware.sh.blackhole.view.ILoginView;

/**
 * Created by Administrator on 2016/12/1 0001.
 */
public class FourActivity extends BaseActivity implements ILoginView, View.OnClickListener{


    private EditText et_username, et_pwd;

    private Button btn_login, btn_reg, btn_clear;

    private ProgressBar pb;

    private ILoginPresenter loginPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_four;
    }

    @Override
    public void initView() {
        super.initView();
        et_username = (EditText) findViewById(R.id.et_username);
        et_pwd = (EditText) findViewById(R.id.et_pwd);

        btn_login = (Button) findViewById(R.id.btn_login);
        btn_reg = (Button) findViewById(R.id.btn_reg);
        btn_clear = (Button) findViewById(R.id.btn_clear);

        pb = (ProgressBar) findViewById(R.id.pb);

        loginPresenter = new ILoginPresenterImpl(this);
    }

    @Override
    public void setListener() {
        super.setListener();

        btn_login.setOnClickListener(this);
        btn_reg.setOnClickListener(this);
        btn_clear.setOnClickListener(this);

    }

    @Override
    public void clear() {
        et_username.setText("");
        et_pwd.setText("");
    }

    @Override
    public void loginResult(boolean isSuccess, String msg) {
        loginPresenter.setProgressBarVisiblity(View.GONE);
        String str = msg;
        if(isSuccess){
            str = "登陆成功";
        }
        Toast.makeText(mContext, str, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void setProgressBarVisiblity(int visiblity) {
        pb.setVisibility(visiblity);
    }

    @Override
    public void regResult(boolean isSuccess, String msg) {
        loginPresenter.setProgressBarVisiblity(View.GONE);
        String str = msg;
        if(isSuccess){
            str = "注册成功";
        }
        Toast.makeText(mContext, str, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View view) {
                String username = et_username.getText().toString().trim();
                String pwd = et_pwd.getText().toString().trim();
        switch (view.getId()){
            case R.id.btn_login:
                if(TextUtils.isEmpty(username)){
                    Toast.makeText(mContext, "用户名不能为空!", Toast.LENGTH_SHORT).show();
                    return ;
                }

                if(TextUtils.isEmpty(pwd)){
                    Toast.makeText(mContext, "密码不能为空!", Toast.LENGTH_SHORT).show();
                    return ;
                }

                loginPresenter.doLogin(username, pwd);
                break;
            case R.id.btn_reg:
                if(TextUtils.isEmpty(username)){
                    Toast.makeText(mContext, "用户名不能为空!", Toast.LENGTH_SHORT).show();
                    return ;
                }

                if(TextUtils.isEmpty(pwd)){
                    Toast.makeText(mContext, "密码不能为空!", Toast.LENGTH_SHORT).show();
                    return ;
                }

                loginPresenter.doReg(username, pwd);
                break;
            case R.id.btn_clear:
                loginPresenter.clear();
                break;
            default:
                break;
        }
    }
}
